package com.software.myopiachildren.ui;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.software.myopiachildren.App;
import com.software.myopiachildren.R;
import com.software.myopiachildren.base.BaseActivity;
import com.software.myopiachildren.bean.BaseInfo;
import com.software.myopiachildren.bean.LoginInfo;
import com.software.myopiachildren.bean.UserInfo;
import com.software.myopiachildren.content.Constant;
import com.software.myopiachildren.http.HostType;
import com.software.myopiachildren.http.HttpApi2;
import com.software.myopiachildren.utils.ToastUtil;
import com.software.myopiachildren.utils.eb.EB;
import com.software.myopiachildren.utils.eb.EbData;
import com.software.myopiachildren.utils.perm.NetworkUtils;
import com.software.myopiachildren.utils.sp.SP;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * 校正距离页
 *
 * @author penglei
 * @date 2020/05/13
 */
public class CorrectionDistanceActivity extends BaseActivity {
    private static final String TAG = "CorrectionDistanceActivity";

    @BindView(R.id.iv_title_left)
    ImageView mIvTitleLeft;
    @BindView(R.id.tv_title_text)
    TextView mTvTitleText;
    @BindView(R.id.tv_title_right)
    TextView mTvTitleRight;
    @BindView(R.id.lay_title_home)
    LinearLayout mLayTitleHome;

    @BindView(R.id.tv_correction_sm)
    TextView mTvCorrectionSm;
    @BindView(R.id.tv_correction_no)
    TextView mTvCorrectionNo;
    @BindView(R.id.ll_corr_status1)
    LinearLayout mLlCorrStatus1;
    @BindView(R.id.tv_correction_already)
    TextView mTvCorrectionAlready;
    @BindView(R.id.tv_correction_number)
    TextView mTvCorrectionNumber;
    @BindView(R.id.tv_correction_unit)
    TextView mTvCorrectionUnit;
    @BindView(R.id.ll_corr_status2)
    LinearLayout mLlCorrStatus2;
    @BindView(R.id.btn_correction)
    Button mBtnCorrection;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_correction_distance;
    }

    @Override
    public void initView() {
        mTvTitleText.setText(getResources().getString(R.string.s_correction_distance));
    }

    @Override
    public void initData() {
        getDistance();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @OnClick({R.id.iv_title_left, R.id.btn_correction})
    public void onClick(final View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;

            case R.id.btn_correction:
                getDistance("20");
                break;
        }
    }

    /**
     * 矫正距离
     */
    private void getDistance() {
        try {
            // 矫正距离
            int corr_dis = new SP().load(Constant.CORR_DIS, 0);
            mTvCorrectionNumber.setText(corr_dis + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 设置用户距离
     */
    private void getDistance(String safe_distance) {
        String jwt_token = new SP().load(Constant.USER_JWT_TOKEN, "");
        if (NetworkUtils.isConnected(CorrectionDistanceActivity.this)) {
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", jwt_token);

            Map<String, String> params = new HashMap<>();
            params.put("safe_distance", safe_distance);

            HttpApi2.getApiService(HostType.TELE_CONFIG).getDistance(headers, params).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<BaseInfo>() {
                        @Override
                        public void onSubscribe(Subscription s) {
                            s.request(Long.MAX_VALUE);
                        }

                        @Override
                        public void onNext(BaseInfo baseInfo) {
                            try {
                                Log.e(TAG, "===baseInfo:" + baseInfo.toString());
                                if (baseInfo.getCode().equals(Constant.CODE_SUCCESS)) {
                                    ToastUtil.showShort(baseInfo.getMsg());
                                    getUserInfo();
                                } else {
                                    ToastUtil.showShort(baseInfo.getMsg());
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(Throwable t) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        } else {
            ToastUtil.showShort(getResources().getString(R.string.network_fial_message));
        }


    }

    /**
     * 用户信息
     */
    private void getUserInfo() {
        String jwt_token = new SP().load(Constant.USER_JWT_TOKEN, "");
        if (NetworkUtils.isConnected(CorrectionDistanceActivity.this)) {
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", jwt_token);

            HttpApi2.getApiService(HostType.TELE_CONFIG).getUserInfo(headers).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<LoginInfo>() {
                        @Override
                        public void onSubscribe(Subscription s) {
                            s.request(Long.MAX_VALUE);
                        }

                        @Override
                        public void onNext(LoginInfo loginInfo) {
                            try {
                                Log.e(TAG, "===loginInfo:" + loginInfo.toString());
                                if (loginInfo.getCode().equals(Constant.CODE_SUCCESS)) {
                                    App.user = loginInfo.getData(); // 用户参数
                                    new SP().save(Constant.USER_ID, loginInfo.getData().getChildren_user_id());
                                    new SP().save(Constant.USER_NAME, loginInfo.getData().getNickname());
                                    new SP().save(Constant.CORR_DIS, loginInfo.getData().getSafe_distance());
                                    new SP().save(Constant.SP_USER_DATA, new Gson().toJson(loginInfo.getData()));

                                    mTvCorrectionNumber.setText(loginInfo.getData().getSafe_distance() + "");
                                    EB.post(new EbData(13));
                                } else {
                                    ToastUtil.showShort(loginInfo.getMsg());
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(Throwable t) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        } else {
            ToastUtil.showShort(getResources().getString(R.string.network_fial_message));
        }
    }

}
